import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import { RouteObject } from "./interface";
import Login from "@/views/login/index.vue";

// * 导入所有router
const modules = import.meta.glob("@/routers/modules/*.ts", { eager: true });

export const routerArray: RouteObject[] = [];
Object.keys(modules).forEach((item) => {
  Object.keys(modules[item] as any).forEach((key: any) => {
    routerArray.push(...(modules[item] as any)[key]);
  });
});

export const rootRouter: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: Login,
    meta: {
      requiresAuth: false,
      title: "登录页",
      key: "login",
    },
  },
  ...routerArray,
  {
    path: "/:pathMatch(.*)*",
    component: () => import("@/views/error/404.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes: rootRouter,
});

export default router;